package com.change.declare.declare.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.change.common.Enums.DeclareFlag;
import com.change.common.Enums.InvoiceStatus;
import com.change.common.Enums.OrderBillType;
import com.change.common.Enums.OrderStatus;
import com.change.declare.declare.BO.MainOrderIDGenerator;
import com.change.declare.declare.BO.TinyOrderIDGenerator;
import com.change.declare.declare.VO.OrderItemVO;
import com.change.declare.declare.VO.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.change.declare.declare.entity.OrdersEntity;
import com.change.declare.declare.service.OrdersService;
import com.change.common.utils.PageUtils;
import com.change.common.utils.R;



/**
 * 
 *
 * @author LK
 * @email hmscrdlk@gmail.com
 * @date 2020-07-11 23:05:53
 */
@RestController
@RequestMapping("declare/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private MainOrderIDGenerator mainOrderIDGenerator;
    @Autowired
    private TinyOrderIDGenerator tinyOrderIDGenerator;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("declare:orders:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = ordersService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("declare:orders:info")
    public R info(@PathVariable("id") Long id){
		OrdersEntity orders = ordersService.getById(id);

        return R.ok().put("orders", orders);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("declare:orders:save")
    public R save(@RequestBody OrdersEntity orders){
		ordersService.save(orders);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("declare:orders:update")
    public R update(@RequestBody OrdersEntity orders){
		ordersService.updateById(orders);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("declare:orders:delete")
    public R delete(@RequestBody Long[] ids){
		ordersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/AddNewOrder")
     public R AddNewOrder(@RequestBody OrderVo order){
        String MainOrderSN = mainOrderIDGenerator.MainOrderSN();
        OrdersEntity ordersEntity = new OrdersEntity();
        ordersEntity.setOrdersn(MainOrderSN);
        ordersEntity.setAdminid(123L);
        ordersEntity.setAdminsn("Admin001");
        ordersEntity.setUserid(456L);
        ordersEntity.setClientid(789L);
        ordersEntity.setClientagreementid(123456l);
        ordersEntity.setCurrency(order.getCurrency());
        ordersEntity.setIsfullvehicle(order.isFullVehicle);
        ordersEntity.setIsloan(order.isLoan);
        ordersEntity.setPackno(order.packNO);
        ordersEntity.setWarptype(order.wrapType);
        BigDecimal totalPrice = order.orderItems.stream().map(OrderItemVO::getTotalPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
        ordersEntity.setDeclareprice(totalPrice);
        ordersEntity.setInvoicestatus(InvoiceStatus.UnInvoiced.getIndex());
        ordersEntity.setIshangup(Boolean.FALSE);
        ordersEntity.setPaidexchangeamount(new BigDecimal(0));
        ordersEntity.setOrderstatus(OrderStatus.Draft.getIndex());
        ordersEntity.setOrderbilltype(OrderBillType.Normal.getIndex());
        ordersEntity.setDeclareflag(DeclareFlag.Unable.getIndex());
        ordersEntity.setDeleted(Boolean.FALSE);
        ordersEntity.setVersion(1);
        Date dtNow = new Date();
        ordersEntity.setCreatetime(dtNow);
        ordersEntity.setUpdatetime(dtNow);

        ordersService.save(ordersEntity);

        return R.ok();
     }
}
